import Vue from 'vue'
import Router from 'vue-router'
import iView from 'iview';
import routes from './router'
import Wcore from '@/libs/common.js';
import store from '@/store';

Vue.use(Router)

const router = new Router({
  routes: routes
})

export default router;

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();

	var farr = ['login','reg'];
	if(farr.in_array(to.name)){
		next();
		return ;
	}
	
	var token = Wcore.getData(Wcore.prefix('token'));
	
	if(!token){
		store.state.user.is_login = false;
		next({name:'login'});
		return;
	}
	else{
		var is_login = store.state.user.is_login;
		
		
		if(!is_login){
			Whttp.load('Admin/Admin/get_my_info',{},function(result){
				store.commit('SET_USER',result);
				next();
			},function(result){
				if(result.result_code==400){
					store.state.user.is_login = false;
					next({name:'login'});
				}
			});
			return;
		}
		else{
			console.log('已经登录');
		}
	}
	
    next();
});

router.afterEach((to) => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});
